﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace ZK.MyControl
{
    public partial class MyGroupBox : GroupBox
    {

        private GraphicsPath gpRegion = null;
        //private bool _mouseHover = false;
        //private bool _mouseDown = false;
        private int textHeight = 0;  //文本高度

        public MyGroupBox()
        {
            InitializeComponent();


            gpRegion = CreateRadiusRect(this.ClientRectangle, radius);
            if (gpRegion != null)
            {
                this.Region = new Region(gpRegion);
            }
            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer | ControlStyles.UserPaint, true);
        }

        #region 圆角大小 Property
        private int radius = 15;
        public int Radius
        {
            get
            {
                return radius;
            }
            set
            {
                if (value < 0)
                {
                    return;
                }
                else
                {
                    radius = value;
                }
            }
        }
        #endregion

        #region 边线颜色
        private Color borderColor = Color.Black;
        public Color BorderColor
        {
            get { return borderColor; }
            set { borderColor = value; }
        }

        #endregion

        #region 上部 颜色
       // private Color uShineColor = Color.FromArgb(40, 132, 194);
        private Color uShineColor = Color.FromArgb(46, 50, 59);
        public Color UShineColor
        {
            get { return uShineColor; }
            set { uShineColor = value; }
        }
        #endregion


        #region 标题字体颜色
        private Color titleColor = Color.White;
        public Color TitleColor
        {
            get { return titleColor; }
            set { titleColor = value; }
        }
        #endregion

        private int titleheight = 38;
        public int TitleHeight
        {
            get { return titleheight; }
            set { titleheight = value; }
        }

        /// <summary>
        /// 描绘具有圆角的button原型路径
        /// </summary>
        protected virtual GraphicsPath CreateRadiusRect(Rectangle rectangle, int radius)
        {
            GraphicsPath path = new GraphicsPath();

            int l = rectangle.Left;  //左边
            int t = rectangle.Top; //上边
            int w = rectangle.Width; //宽度
            int h = rectangle.Height;// 高度
            int d = radius / 2; //除以2，得到半径

            path.AddArc(l, t, d, d, 180, 90);  //左上角
            path.AddLine(l + radius, t, l + w - radius, t);//上边线

            path.AddArc(l + w - d, t, d, d, 270, 90);//右上角
            path.AddLine(l + w, t + radius, l + w, t + h - radius); //右边线

            path.AddArc(l + w - d, t + h - d, d, d, 0, 90);//右下角
            path.AddLine(l + w - radius, t + h, l + radius, t + h); //下边线

            path.AddArc(l, t + h - d, d, d, 90, 90); //左下角
            path.AddLine(l, t + h - radius, l, t + radius); //左边线

            path.CloseFigure();
            return path;
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            gpRegion = CreateRadiusRect(this.ClientRectangle, radius);
            if (gpRegion != null)
            {
                this.Region = new Region(gpRegion);
            }
            base.OnSizeChanged(e);
        }

        //按照button路径进行border绘制
        protected virtual void DrawBorder(Graphics g, Pen pen)
        {
            if (gpRegion != null)
            {
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                g.DrawPath(pen, CreateRadiusRect(new Rectangle(this.ClientRectangle.Left, this.ClientRectangle.Top, this.ClientRectangle.Width - 1, this.ClientRectangle.Height - 1), radius));
                g.SmoothingMode = SmoothingMode.Default;
                g.InterpolationMode = InterpolationMode.Default;
            }
            pen.Dispose();
            pen = null;
        }

        //绘制上部渐变色
        protected virtual void DrawUpperShine(Graphics g)
        {
            //Color c1 = Color.FromArgb(200, uShineColor);
            //Color c2 = Color.FromArgb(100, Color.White);
            //Rectangle rc = new Rectangle(this.ClientRectangle.Left, this.ClientRectangle.Top, Width - 1, Height / 2);
            //if (_mouseDown)
            //{
            //    rc = new Rectangle(this.ClientRectangle.Left, this.ClientRectangle.Top, Width - 1, (Height - 10) / 2);
            //}
            //using (LinearGradientBrush lbrush = new LinearGradientBrush(rc, c1, c2, LinearGradientMode.Vertical))
            //{
            //    g.FillRectangle(lbrush, rc);
            //}
        }

        //设置字体的大小，宽高，位置信息
        protected virtual void DrawText(Graphics g)
        {
            if (!String.IsNullOrEmpty(this.Text.Trim()))
            {
                int x = 0;
                int y = 0;
                int w = 0;
                int h = 0;

                Size size = g.MeasureString(Text, Font).ToSize();
                w = size.Width > Width ? Width : size.Width;
                h = size.Height > Height ? Height : size.Height;

                Pen pen = new Pen(TitleColor);
                RectangleF rectangleF = new RectangleF((float)x + 10, (float)y +10, (float)w + 3, (float)h);
             
                g.DrawString(Text, Font, pen.Brush, rectangleF);
                pen.Dispose();
                pen = null;
                textHeight = w;
            }
        }

        ////绘制内部的方块
        //protected virtual void DrawInner(Graphics g, Rectangle rectangle, Pen p)
        //{
        //    //int l = rectangle.Left; //左边
        //    //int t = rectangle.Top;//上边
        //    //int w = rectangle.Width;//宽度
        //    //int h = rectangle.Height;//高度

        //    //g.DrawLine(p, l + 10, t + 20, l + w - 10, t + 20);//上边线
        //    //g.DrawLine(p, l + w - 10, t + 20, l + w - 10, t + h - 10);  //右边线
        //    //g.DrawLine(p, l + w - 10, t + h - 10, l + 10, t + h - 10);  //下边线
        //    //g.DrawLine(p, l + 10, t + h - 10, l + 10, t + 20); //左边线

        //    //Rectangle r = new Rectangle(l + 10, t + 20, w - 18, h - 28);


        //    //g.FillRectangle(new SolidBrush(Color.Wheat), r);
        //}
    
        protected virtual void DrawTopPanel(Graphics g )
        {
            Size size = g.MeasureString(Text, Font).ToSize();

            Rectangle rectangle = new Rectangle(0, 0, this.Width, TitleHeight);

            Brush brush = new SolidBrush(UShineColor);
            g.FillRectangle(brush, rectangle);

             this.Padding = new Padding(this.Padding.Left, 18, this.Padding.Right, this.Padding.Bottom);
        }


        protected override void OnPaint(PaintEventArgs e)
        {
            Pen pen = new Pen(borderColor);
            Brush brush = new SolidBrush(BackColor);
            e.Graphics.FillPath(brush, gpRegion);

            DrawTopPanel(e.Graphics);
            DrawText(e.Graphics);
            DrawBorder(e.Graphics, pen);
            DrawUpperShine(e.Graphics);
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // MyGroupBox
            // 
            this.BackColor = System.Drawing.Color.White;
            this.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.ForeColor = System.Drawing.Color.Black;
            this.Text = "MyGroupBox";
            this.ResumeLayout(false);

        }
    }
}
